package com.zlf.controller;

import com.zlf.po.CommonResult;
import com.zlf.po.User;
import com.zlf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RefreshScope  //开启动态刷新
@RequestMapping("/UserController")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserByIdByPass/{userId}/{password}")
    public CommonResult<User> getUserByIdByPass(
            @PathVariable("userId") String userId,
            @PathVariable("password") String password) throws Exception{
        User param=new User();
        param.setUserId(userId);
        param.setPassword(password);
        User user=userService.getUserByIdByPass(param);
        return new CommonResult<>(200,"success",user);
    }

    @GetMapping("/getUserById/{userId}")
    public CommonResult<User> getUserById(@PathVariable("userId") String userId) throws
            Exception{
        User result = userService.getUserById(userId);
        return new CommonResult<>(200,"success",result);
    }

    @PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}")
    public CommonResult<Integer> saveUser(
            @PathVariable("userId") String userId,
            @PathVariable("password") String password,
            @PathVariable("userName") String userName,
            @PathVariable("userSex") Integer userSex) throws Exception{
        User param = new User();
        param.setUserId(userId);
        param.setPassword(password);
        param.setUserName(userName);
        param.setUserSex(userSex);
        int result = userService.saveUser(param);
        return new CommonResult(200,"success",result);
    }


    @PostMapping("/updateUserPass/{newPassword}/{userId}/{oldPassword}")
    public CommonResult<Integer> updateUserPass(
            @PathVariable("newPassword") String newPassword,
            @PathVariable("userId") String userId,
            @PathVariable("oldPassword") String oldPassword) throws Exception{
        int result=userService.updateUserPass(newPassword,userId,oldPassword);
        return new CommonResult(200,"success",result);
    }
    @PostMapping("/updateNickname/{userId}/{newNickname}")
    public CommonResult<Integer> updateNickname(
            @PathVariable("userId") String userId,
            @PathVariable("newNickname") String newNickname)
    {
        int result= userService.updateNickname(userId,newNickname);
        return new CommonResult(200,"success",result);
    }
    @PostMapping("/uploadAvatarBase64/{userId}/{avatarBase64}")
    public CommonResult<Integer> uploadAvatarBase64(
            @PathVariable("userId") String userId,
            @PathVariable("avatarBase64") String avatarBase64)
    {
        int result= userService.uploadAvatarBase64(userId,avatarBase64);
        return new CommonResult(200,"success",result);
    }


}